package com.sunseen.intelligencebus.user;

import android.util.Log;
import com.avos.avoscloud.*;
import org.greenrobot.eventbus.EventBus;

public class BalanceManager {

    public static void addBalance(final double addValue) {
        AVQuery<Account> query = new AVQuery<>("Account");
        query.whereEqualTo("name", AVUser.getCurrentUser().getUsername());
        query.getFirstInBackground(new GetCallback<Account>() {
            @Override
            public void done(final Account account, AVException e) {
                if (account != null) {
                    account.increment("balance", addValue);
                    account.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(AVException e) {
                            if (e == null) {
                                Log.e("test", "充值后，余额为：" + account.get("balance"));
                                EventBus.getDefault().post(new BalanceUpdateEvent((Double) account.get("balance")));
                            } else {
                                Log.e("test", e.getMessage());
                                EventBus.getDefault().post(new BalanceUpdateFailedEvent());
                            }
                        }
                    });
                } else {
                    EventBus.getDefault().post(new BalanceUpdateFailedEvent());
                }

            }
        });
    }

    public static void cutBalance(final double cutValue) {
        AVQuery<Account> query = new AVQuery<>("Account");
        query.whereEqualTo("name", AVUser.getCurrentUser().getUsername());
        query.getFirstInBackground(new GetCallback<Account>() {
            @Override
            public void done(final Account account, AVException e) {
                if (account != null) {
                    account.increment("balance", -cutValue);
                    account.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(AVException e) {
                            if (e == null) {
                                Log.e("test", "消费后，余额为：" + account.get("balance"));
                                EventBus.getDefault().post(new BalanceUpdateEvent((Double) account.get("balance")));
                            } else {
                                Log.e("test", e.getMessage());
                                EventBus.getDefault().post(new BalanceUpdateFailedEvent());
                            }
                        }
                    });
                } else {
                    EventBus.getDefault().post(new BalanceUpdateFailedEvent());
                }
            }
        });
    }

    public static void requestBalance() {
        AVQuery<Account> query = new AVQuery<>("Account");
        query.whereEqualTo("name", AVUser.getCurrentUser().getUsername());
        query.getFirstInBackground(new GetCallback<Account>() {
            @Override
            public void done(final Account account, AVException e) {
                if (account != null) {
                    EventBus.getDefault().post(new BalanceGotEvent(account.getBlance()));
                } else {
                    EventBus.getDefault().post(new BalanceGotEvent(0));
                }
            }
        });
    }
}
